/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.commons.core.jackson;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * 自定义Jackson对数组、字符串、数值、布尔和实体为null的序列化；
 *
 * @author Jasmine
 */
public class NullJsonComponent {

    public final static JsonSerializer<Object> NULL_ARRAY_SERIALIZER = new NullArrayJsonSerializer();

    public final static JsonSerializer<Object> NULL_STRING_SERIALIZER = new NullStringJsonSerializer();

    public final static JsonSerializer<Object> NULL_NUMBER_SERIALIZER = new NullNumberJsonSerializer();

    public final static JsonSerializer<Object> NULL_BOOLEAN_SERIALIZER = new NullBooleanJsonSerializer();

    public final static JsonSerializer<Object> NULL_OBJECT_SERIALIZER = new NullObjectJsonSerializer();

    /**
     * 处理数组集合类型的null值
     */
    public static class NullArrayJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {
            if (value == null) {
                gen.writeStartArray();
                gen.writeEndArray();
            } else {
                gen.writeObject(value);
            }
        }
    }

    /**
     * 处理字符串类型的null值
     */
    public static class NullStringJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {
            if (value == null) {
                gen.writeString("");
            } else {
                gen.writeObject(value);
            }
        }
    }

    /**
     * 处理数值类型的null值
     */
    public static class NullNumberJsonSerializer extends JsonSerializer<Object> {

        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            if (value == null) {
                gen.writeNumber(0);
            } else {
                gen.writeObject(value);
            }
        }
    }

    /**
     * 处理boolean类型的null值
     */
    public static class NullBooleanJsonSerializer extends JsonSerializer<Object> {

        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            if (value == null) {
                gen.writeBoolean(false);
            } else {
                gen.writeObject(value);
            }
        }
    }

    /**
     * 处理实体对象类型的null值
     */
    public static class NullObjectJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializerProvider)
            throws IOException {
            if (value == null) {
                gen.writeStartObject();
                gen.writeEndObject();
            } else {
                gen.writeObject(value);
            }
        }
    }

    /**
     * 处理实体对象类型为null值 -> null
     */
    public static class NullJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
            jsonGenerator.writeNull();
        }
    }
}